<?php
/**
 * Created by PHPStorm
 * User: xiest
 * Date: 2021/06/15
 * Email: <939369942@qq.com>
 */
namespace basic\services;

use basic\services\upload\storage\Oss;
use basic\services\upload\Upload;
use think\facade\Config;

/**
 * @mixin  Oss
 */
class UploadService
{
    protected static $upload = [];

    /**
     * @param null $type
     * @return Upload|mixed
     * User: xiest
     * Date: 2021/4/6
     */
    public static function create($type=null)
    {
        $type = $type ?: (systemConfig('upload_type') ?:'local');
        $config = [];
        if($type == 'oss'){
            $tmpConfig = systemConfig(['aliyun_oss_access_key','aliyun_oss_secret_key','aliyun_oss_upload_url','aliyun_oss_storage_name','aliyun_oss_storage_region']);
            $config = [
             'accessKey'=>$tmpConfig['aliyun_oss_access_key'],
             'secretKey'=>$tmpConfig['aliyun_oss_secret_key'],
             'uploadUrl'=>$tmpConfig['aliyun_oss_upload_url'],
             'storageName'=>$tmpConfig['aliyun_oss_storage_name'],
             'storageRegion'=>$tmpConfig['aliyun_oss_storage_region'],
            ];
        }else $type = 'local';

        if(isset(self::$upload['upload_'.$type]))
            return self::$upload['upload_'.$type];
        return self::$upload['upload_' . $type] = new Upload($type, $config);
    }
}